package com.vote.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vote.vote.domain.TbVote;
import com.vote.vote.mapper.TbVoteDao;
import com.vote.vote.service.TbVoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * (TbVote)表服务实现类
 *
 * @author makejava
 * @since 2023-09-26 10:23:32
 */
@Service
@Transactional
public class TbVoteServiceImpl extends ServiceImpl<TbVoteDao, TbVote> implements TbVoteService {
    private static final Logger log = LoggerFactory.getLogger(TbVoteServiceImpl.class);
    @Resource
    private TbVoteDao tbVoteDao;


    @Override
    public List<TbVote> queryList(TbVote tbVote) {
//        获取所有视频 用户只能获取自己的视频
        List<TbVote> tbVotes = tbVoteDao.queryList(tbVote);
//      TODO  自己的视频


        return tbVotes;
    }

    @Override
    public Integer updateVoteById(TbVote tbVote) {

        Integer integer = tbVoteDao.updateVoteById(tbVote);

        return integer;
    }

    @Override
    public List<ConcurrentHashMap<String, Object>> getRanking() {

        return tbVoteDao.getRanking();
    }

    @Override
    public Integer insertVote(TbVote tbVote) {
        //先查询 该用户是否 发布过
        TbVote vote = tbVoteDao.selectOne(new QueryWrapper<TbVote>().eq("v_uid", tbVote.getvUid()));
        //TODO 为空添加  否则 修改
        if (vote == null) return tbVoteDao.insert(tbVote);

        return tbVoteDao.updateVoteById(tbVote);

    }
}

